package Controle2;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Modelo.Cliente;
import Modelo.Locacao;
import Modelo.Veiculo;

/**
 * Servlet implementation class ControleLocacao
 */
@WebServlet("/ControleLocacao")
public class ControleLocacao extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ControleLocacao() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.getRequestDispatcher("Locacao.jsp").forward(request,
				response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String operacao = request.getParameter("tipo");
		try {
			if(operacao.equals("Verificar")){
				String a = request.getParameter("cpf/cnpj");
				if(a.equals("cpf")){
					Cliente v = new Cliente();
					v.setCpf(request.getParameter("cpf"));
					
					if(v.validaCpf()){
						Veiculo b = new Veiculo();
						b.setPlaca(request.getParameter("numeroplaca"));
					
						if(b.validaPlaca()){
							request.getSession().setAttribute("cpf/cnpj", v.getCnpj());
							request.getSession().setAttribute("numerplaca", b.getPlaca());
							request.getRequestDispatcher("Comfirmarlocacao.jsp").forward(request, response);
						}else{
							request.getSession().setAttribute("tipo", new String("Locacao.jsp"));
							request.getSession().setAttribute("messagen",new String("Erro placa nao existente no sistema.") );
							request.getRequestDispatcher("Erro.jsp").forward(request, response);
						}
					}else{
						request.getSession().setAttribute("tipo", new String("Locacao.jsp"));
						request.getSession().setAttribute("messagen",new String("Erro Cliente nao existe cadastrar o mesmo.") );
						request.getRequestDispatcher("Erro.jsp").forward(request, response);
					}
					
				}else{
					Cliente v = new Cliente();
					v.setCnpj(request.getParameter("cnpj"));
				
					if(v.validaCnpj()){
						Veiculo c = new Veiculo();
						c.setPlaca(request.getParameter("numeroplaca"));
					
						if(c.validaPlaca()){
							request.getSession().setAttribute("cpf/cnpj", v.getCnpj());
							request.getSession().setAttribute("numerplaca", c.getPlaca());
							request.getRequestDispatcher("Comfirmarlocacao.jsp").forward(request, response);
						}else{
							request.getSession().setAttribute("tipo", new String("Locacao.jsp"));
							request.getSession().setAttribute("messagen",new String("Erro placa nao existente no sistema.") );
							request.getRequestDispatcher("Erro.jsp").forward(request, response);
						}
					}else{
						request.getSession().setAttribute("tipo", new String("Locacao.jsp"));
						request.getSession().setAttribute("messagen",new String("Erro Cliente nao existe cadastrar o mesmo.") );
						request.getRequestDispatcher("Erro.jsp").forward(request, response);
					}
				}
			}
			else if(operacao.equals("Inserir locacao")){
				Locacao v = new Locacao();
				v.setDataEmpre(request.getParameter("dataemp"));
				v.setLocalEmpre(request.getParameter("localemp"));
				v.setDataDev(request.getParameter("datadev"));
				v.setLocalPrevDev(request.getParameter("localprev"));
				v.setTipoTarifa(request.getParameter("tipotarifa"));
				v.setCpf(request.getParameter("cpf/cnpj"));
				v.setPlaca(request.getParameter("placa"));
				
				//add o veiculo no banco via a classe VeiculoDAO
				//mudar vdao.adicionar(v) para um metodo q retorna true ou false
				if(v.adicionarLocacao()){
					request.getSession().setAttribute("tipo", new String("InserirVeiculo.jsp"));
					request.getSession().setAttribute("messagen",new String("Foi efetuado o cadastro da locacao.") );
					request.getRequestDispatcher("Sucesso.jsp").forward(request, response);
				}else{
					request.getSession().setAttribute("tipo", new String("VerificarPlaca.jsp"));
					request.getSession().setAttribute("messagen",new String("Erro em cadastrar uma locacao tentar novamente.") );
					request.getRequestDispatcher("Erro.jsp").forward(request, response);
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
